package com.example.backend.service.impl;

import com.example.backend.mapper.FoodMapper;
import com.example.backend.mapper.OrderMapper;
import com.example.backend.mapper.RestaurantMapper;
import com.example.backend.mapper.UserMapper;
import com.example.backend.pojo.*;
import com.example.backend.service.OrderService;
import com.example.backend.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RestaurantMapper restaurantMapper;
    @Autowired
    private FoodMapper foodMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void addOrder(Foodorder foodorder) {
        Integer orderid= orderMapper.count()+1;//获得当前订单号
        foodorder.setOrderid(orderid);

        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");//用户进行购物，是用户的id
        String username = (String) map.get("username");

        foodorder.setUserid(id);
        orderMapper.addOrder(foodorder);//下单

        //获得下单人的信息
        User user = userMapper.getUserByName(username);
        List<Integer> res = userMapper.ser(user.getUsertypeid());
        System.out.println(res);

        Integer first=null,second=null,third=null;
        if(res.size()==1){
            first=res.get(0);
        }else if(res.size()==2)
        {
            first=res.get(0);
            second=res.get(1);
        }else {
            first=res.get(0);
            second=res.get(1);
            third=res.get(2);
        }
        userMapper.updateusertype(first,second,third,user.getUsertypeid());

        for (Orderdetail detail:foodorder.getOrders()){
            detail.setOrderid(orderid);
            orderMapper.adddetail(detail);
            Food food = foodMapper.getbyfoodid(Integer.valueOf(detail.getFoodid()));
            foodMapper.updatesale(Integer.valueOf(detail.getFoodid()),food.getSales()+detail.getNumber());
        }
    }

    @Override
    public List<Foodorder> getuserorder(Integer id) {
        List<Foodorder> list = orderMapper.getuserorder(id);//获取用户订单
        for (Foodorder f:list){
            Restaurant r = restaurantMapper.getRestaurantById(f.getRestaurantid());
            f.setRestaurantid(r.getRestaurantname());
        }
        return  list;
    }

    @Override
    public List<Orderdetail> getuserorderdetail(Integer id) {
        List<Orderdetail> orderdetails = orderMapper.getuserorderdetail(id);
        for (Orderdetail o:orderdetails){
            Food f = foodMapper.getbyfoodid(Integer.valueOf(o.getFoodid()));
            o.setFoodid(f.getFoodname());
            o.setImage(f.getImage());
        }
        return orderdetails;
    }

    @Override
    public List<Foodorder> getuserorderx(Integer id, Integer state) {
        List<Foodorder> list =orderMapper.getuserorderx(id, state);
        for (Foodorder f:list){
            Restaurant r = restaurantMapper.getRestaurantById(f.getRestaurantid());
            f.setRestaurantid(r.getRestaurantname());
        }
        return  list;
    }

    @Override
    public List<Foodorder> getresorder(String id, Integer state) {
        List<Foodorder> list =orderMapper.getresorder( id, state);
        for (Foodorder f:list){
            Restaurant r = restaurantMapper.getRestaurantById(f.getRestaurantid());
            f.setRestaurantid(r.getRestaurantname());
        }
        return  list;
    }

    @Override
    public Foodorder findbyId(Integer orderid) {
        Foodorder f=orderMapper.findbyId(orderid);
        Restaurant r = restaurantMapper.getRestaurantById(f.getRestaurantid());
        f.setRestaurantid(r.getRestaurantname());
        return f;
    }
}
